package de.fhhof.vinf.ssp.ticketcenter.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import de.fhhof.vinf.ssp.ticketcenter.beans.UserBean;
import de.fhhof.vinf.ssp.ticketcenter.helpers.Logger;

/**
 * Application Lifecycle Listener implementation class ObjectBoundListener
 *
 */
@WebListener
public class ObjectBoundListener implements HttpSessionAttributeListener {

    /**
     * Default constructor. 
     */
    public ObjectBoundListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
     */
    public void attributeRemoved(HttpSessionBindingEvent arg0) {
    	String toLog = "Object " + arg0.getValue().toString() + " was removed from Session ID = " + arg0.getSession().getId();
    	Logger.log(toLog);
    }

	/**
     * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
     */
    public void attributeAdded(HttpSessionBindingEvent arg0) {
    	if(arg0.getValue() instanceof UserBean){
    		String toLog = "UserBean an Session ID =" + arg0.getSession().getId() + " gebunden. Werte: UserID = " + ((UserBean)arg0.getValue()).getUserId() + "; E-Mail = " + ((UserBean)arg0.getValue()).getEmail() ;
    		Logger.log(toLog);
    	} else {
    		String toLog = "Object " + arg0.getValue().toString() + " was added to Session ID = " + arg0.getSession().getId();
        	Logger.log(toLog);
    	}
    	
    }

	/**
     * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
     */
    public void attributeReplaced(HttpSessionBindingEvent arg0) {
    	String toLog = "Object " + arg0.getValue().toString() + " was replaced at Session ID = " + arg0.getSession().getId();
    	Logger.log(toLog);
    }
	
}
